package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.IPlatformChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : PayController
 * @Author : 阿泽
 * @Date: 2021/7/29 18:49
 * @Description :支付渠道相关
 */
@RestController
@Api(value = "商户平台-渠道和支付参数相关", tags = "商户平台-渠道和支付参数", description = "商户平台-渠道和支付参数相关")
public class PayController {
    @Reference
    IPlatformChannelService platformChannelService;
    @ApiOperation("获取平台服务类型")
    @GetMapping(value="/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel(){
        return platformChannelService.findAllPlatformChannel();
    }

    @ApiOperation("绑定服务类型")
    @PostMapping(value="/my/apps/{appId}/platform-channels")
    public Object bindPlatformForApp(@PathVariable String appId,@RequestParam("platformChannelCodes")String platformChannel){

        return platformChannelService.bindPlatformForApp(appId,platformChannel);

    }

    @ApiOperation("查询应用是否绑定了某个服务类型")
    @GetMapping("/my/merchants/apps/platformchannels") //@RequestParam默认require=true
    public int queryAppBindPlatformChannel(@RequestParam(required=false) String appId,@RequestParam String platformChannel){
        //required=false关闭非空 使用自定义异常
        if (StringUtil.isEmpty(appId)){
            throw new BusinessException(CommonErrorCode.E_100101);
        }
        return platformChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }

    @ApiOperation("查询服务下的所有支付通道")
    @GetMapping("/my/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelsByPlatfrom(@PathVariable("platformChannelCode") String platformChannelCode){

        return platformChannelService.queryPayChannelsByPlatfrom(platformChannelCode);
    }
    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payChannelParam", value = "商户配置支付渠道参数", required =
                    true, dataType = "PayChannelParamDTO", paramType = "body")
    })
    @RequestMapping(value = "/my/pay-channel-params",method =
            {RequestMethod.POST,RequestMethod.PUT})
    public Object createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParam){
        //判断非空
        if (StringUtil.isEmpty(payChannelParam.getAppId())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (StringUtil.isEmpty(payChannelParam.getChannelName())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (StringUtil.isEmpty(payChannelParam.getParam())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (StringUtil.isEmpty(payChannelParam.getPlatformChannelCode())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (StringUtil.isEmpty(payChannelParam.getPayChannel())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }

        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParam.setMerchantId(merchantId);

        platformChannelService.savePayChannelParam(payChannelParam);
        return "操作成功";
    }
    @ApiOperation("查询某个应用下某个服务的全部支付通道参数")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamDTO> queryPayChannelParam(@PathVariable String appId, @PathVariable String platformChannel) {

        return platformChannelService.queryPayChannelParam(appId,platformChannel);
    }

    @ApiOperation("查询某个应用下某个服务的某个支付通道参数")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
            public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId,
            @PathVariable String platformChannel, @PathVariable String payChannel) {
            return platformChannelService.queryParamByAppPlatformAndPayChannel(appId, platformChannel,
            payChannel);
}
}
